package com.dap.system.office.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.common.core.web.domain.BaseEntity;
import com.dap.system.office.domain.TCourse;
import com.dap.system.office.domain.vo.TCourseSelectorVo;
import com.dap.system.office.domain.vo.TTeacherSelectorVo;
import com.dap.system.office.mapper.TClassMapper;
import com.dap.system.office.mapper.TCourseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TTeacherMapper;
import com.dap.system.office.domain.TTeacher;
import com.dap.system.office.service.ITTeacherService;

import javax.annotation.Resource;

/**
 * 导师Service业务层处理
 *
 * @author dap
 * @date 2021-11-10
 */
@Service
public class TTeacherServiceImpl implements ITTeacherService {
    @Resource
    private TTeacherMapper tTeacherMapper;
    @Resource
    private TCourseMapper tCourseMapper;

    /**
     * 查询导师
     *
     * @param id 导师ID
     * @return 导师
     */
    @Override
    public TTeacher selectTTeacherById(Long id) {
        return tTeacherMapper.selectTTeacherById(id);
    }

    /**
     * 查询导师列表
     *
     * @param tTeacher 导师
     * @return 导师
     */
    @Override
    public List<TTeacher> selectTTeacherList(TTeacher tTeacher) {
        return tTeacherMapper.selectTTeacherList(tTeacher);
    }

    /**
     * 分页查询导师列表
     *
     * @param tTeacher 导师
     * @return 导师
     */
    @Override
    public List<TTeacher> selectTTeacherListPage(TTeacher tTeacher) {

        PageHelper.clearPage();
        PageHelper.startPage(tTeacher.getPageNum(), tTeacher.getPageSize());
        List<TTeacher> list = tTeacherMapper.selectTTeacherList(tTeacher);
        PageInfo<TTeacher> tPageInfo = new PageInfo<>(list);
        List<TTeacher> dataList = tPageInfo.getList();
        return dataList;
    }

    /**
     * 新增导师
     *
     * @param tTeacher 导师
     * @return 结果
     */
    @Override
    public int insertTTeacher(TTeacher tTeacher) {
        TTeacher teacher = new TTeacher();
        teacher.setTeaIdNumber(teacher.getTeaIdNumber());
        List<TTeacher> teachers = tTeacherMapper.selectTTeacherList(teacher);
        if (teachers.size() > 0) {
            throw new CustomException("该教师已存在，修改失败");
        }
        tTeacher.setCreateBy(SecurityUtils.getUsername());
        tTeacher.setCreateTime(DateUtils.getNowDate());
        tTeacher.setDelFlag("0");
        return tTeacherMapper.insertTTeacher(tTeacher);
    }

    /**
     * 修改导师
     *
     * @param tTeacher 导师
     * @return 结果
     */
    @Override
    public int updateTTeacher(TTeacher tTeacher) {
        TTeacher teacher = new TTeacher();
        teacher.setTeaIdNumber(teacher.getTeaIdNumber());
        List<TTeacher> teachers = tTeacherMapper.selectTTeacherList(teacher);
        if (teachers.size() > 0) {
            throw new CustomException("该教师已存在，修改失败");
        }
        tTeacher.setUpdateBy(SecurityUtils.getUsername());
        tTeacher.setUpdateTime(DateUtils.getNowDate());
        return tTeacherMapper.updateTTeacher(tTeacher);
    }

    /**
     * 批量删除导师
     *
     * @param ids 需要删除的导师ID
     * @return 结果
     */
    @Override
    public int deleteTTeacherByIds(Long[] ids) {
        for (Long id : ids) {
            TCourse course=new TCourse();
            course.setTeaName(course.getTeaName());
            List<TCourse> tCourses=tCourseMapper.selectTCourseList(course);
            if (tCourses.size()>0){
                throw new CustomException("该教师有担任的课程，禁止删除");
            }


        }

        return tTeacherMapper.deleteTTeacherByIds(ids);
    }

    /**
     * 删除导师信息
     *
     * @param id 导师ID
     * @return 结果
     */
    @Override
    public int deleteTTeacherById(Long id) {
        return tTeacherMapper.deleteTTeacherById(id);
    }

    @Override
    public List<TTeacherSelectorVo> selectTTeacherNameListPage(BaseEntity baseEntity) {
        PageHelper.clearPage();
        PageHelper.startPage(baseEntity.getPageNum(), baseEntity.getPageSize());
        return tTeacherMapper.selectAllTTeacherNameList();
    }

    @Override
    public Map<String, Object> importTeacher(List<TTeacher> teacherList, boolean updateSupport, String operName) {
        Map<String, Object> res = new HashMap<>();
        if (StringUtils.isNull(teacherList) || teacherList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        } else {
            List<TTeacher> err = new ArrayList<>();
            int upd = 0;
            int ins = 0;
            for (TTeacher teachers : teacherList) {
                if (StringUtils.isNotEmpty(teachers.getTeaIdNumber())) {
                    TTeacher teacher = tTeacherMapper.selectTeaId(teachers.getTeaIdNumber());
                    if (!StringUtils.isNull(teacher)) {
                        teachers.setId(teacher.getId());
                        if ("男".equals(teachers.getTeaSex())) {
                            teachers.setTeaSex("1");
                        } else {
                            teachers.setTeaSex("0");
                        }
                        teachers.setUpdateBy(SecurityUtils.getUsername());
                        teachers.setUpdateTime(DateUtils.getNowDate());
                        tTeacherMapper.updateTTeacher(teachers);
                        upd++;
                    } else {
                        if ("男".equals(teachers.getTeaSex())) {
                            teachers.setTeaSex("1");
                        } else {
                            teachers.setTeaSex("0");
                        }
                        teachers.setCreateTime(DateUtils.getNowDate());
                        teachers.setCreateBy(SecurityUtils.getUsername());
                        teachers.setDelFlag("0");
                        tTeacherMapper.insertTTeacher(teachers);
                        ins++;
                    }
                } else {
                    err.add(teachers);
                }
            }
            res.put("err", err.size());
            res.put("upd", upd);
            res.put("ins", ins);
            return res;
        }
    }
}
